/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import dao.CountryJpaController;
import entities.Country;
import java.util.List;

public class CountryService {
    private static CountryJpaController cc;
    
    private CountryJpaController getJPAController()
    {
        if(cc == null)
            cc = new CountryJpaController();
        return cc;
    }
    
    public boolean saveCountry(String name)
    {
        boolean ret = true;
        cc = getJPAController();
        try
        {
            cc.create(new Country(0,name));
        }
        catch (Exception e)
        {
            ret = false;
        }
        return ret;
    }
    public Country getCountry(String name)
    {
        cc = getJPAController();
        return cc.getCountryByName(name);
    }
    
    public boolean editCountry(Country c)
    {
        cc = getJPAController();
        try
        {
            cc.edit(c);
            return true;
        } catch (Exception e)//Country doesn't exist initially
        {
            return false;
        }
    }

    public boolean deleteCountry(Country c)
    {
        cc = getJPAController();
        try
        {
            cc.destroy(c.getIdCountry());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public List<Country> findAll()
    {
        cc = getJPAController();
        try
        {
            return cc.findAll();
        } catch (Exception e)
        {
            return null;
        }
    }
}
